package com.ysm.smfu.utils;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpHelper {
//    网络请求到BaseUrl
    public static final String BASEURL = "http://127.0.0.1:8090";
    private OkHttpClient okHttpClient = new OkHttpClient();
    private String url;

    public HttpHelper(String url) {
        this.url = url;
    }

    /**
     * 同步get请求
     * @return
     */
    public String getSync(){
//     创建请求对象
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = okHttpClient.newCall(request).execute();//同步任务
            if (response.isSuccessful()){ // 200
                return  response.body().string(); // 返回json数据
            }
        } catch (IOException e) {
            System.err.println("异常："+e.getMessage());
        }
        return null;
    }
}
